home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / program / tpwfort.zip / CHCASTPW.PAS < prev    next >
Pascal/Delphi Source File  |  1992-06-11  |  12KB  |  427 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo program using MS FORTRAN 5.1 DLL        }
  5. {   August Miller -> internet: miller@nmsu.edu   }
  6. {                                                }
  7. {************************************************}
  8.  
  9. (*
  10.    This program is that of a really novice Windows and Pascal
  11.    programmer. Most of it was pirated in some form or other from
  12.    the demo programs supplied with Turbo Pascal foe Windows 1.0.
  13.    Borland certainly wasn't responsible for any stupid constructions
  14.    that you might find here, however. . they are mine alone!
  15.  
  16.    I started this when I found that I was pretty disappointed with
  17.    the "Quick Win" interface provided by Microsoft's FORTRAN 5.1 and
  18.    wondered if I might be able to use Turbo Pascal to call up a program
  19.    written in FORTRAN. This program is just a shell and
  20.    does nothing but read an input file, convert all the characters
  21.    to upper case, and them write the results to ANOTHER file..
  22.    There are slicker ways to do that. This was just an experiment,
  23.    but may be of interest to one or two other people.
  24.  
  25.    I began with a FORTRAN subroutine named CHCASW.FOR to which we must
  26.    pass the names of the input and output files as well as an integer
  27.    parameter which specifies whether conversion is to UPPER or lower
  28.    case. CHCASW.FOR was compiled and linked into a DLL called CHCASW.DLL
  29.    The internal (actual) name in the subroutine header is CHCASE. CHCASE
  30.    opens the input and output files, does its job and then closes both
  31.    files. (I did it that way because I didn't have the slightest idea as
  32.    to how to open them in Turbo Pascal and then pass the proper
  33.    logical unit numbers to the FORTRAN subroutine.)
  34.  
  35.    This TP program to solicit names for input and output files and
  36.    to call the CHCASE subroutine in CHCASW.DLL to do the converting and
  37.    file handling. CHCASW.DLL and should be put in your base Windows
  38.    directory before you run this one.
  39.  
  40.    There is an interface program CHCASW.PAS which you must compile
  41.    to produce CHCASW.TPU before compiling this one. The interface
  42.    program is the guts of setting up calls to a FORTRAN dll.. you
  43.    gotta make all the variable types are consistent for both worlds.
  44.  
  45.    Finally, there is CHCASW.RES which contains a menu of sorts:
  46.    The "File" item has two sub items which are used to enter the input
  47.    and output file names. The "Run" item brings up the actual call of
  48.    the subroutine "CHCASE.FOR" which is all that is in CHCASW.DLL.
  49.    Nothing at all appears in the program's main window except the
  50.    file dialog boxes.
  51.  
  52.    The FORTRAN related files are:
  53.    CHCASW.FOR - the source code for the "change case" routine.
  54.    CHCASW.DEF - "definition" file needed to create the DLL.
  55.    CHCASDLL.MAK - the "NMAKE" file to create CHCASW.DLL.
  56. *)
  57.  
  58. program MyProgram;
  59.  
  60. uses Strings, WinTypes, WinProcs, WinDos, WObjects, StdDlgs,chcasw;
  61.  
  62. {$R chcasw.res}
  63.  
  64. const
  65.   cm_new    = 101;
  66.   cm_Open   = 102; {open IOIN file!!}
  67.   cm_save   = 103;
  68.   cm_SaveAs = 104; {open/create IOUT file}
  69.   cm_Help   = 901;
  70.   idm_go    = 200;
  71.   cm_myexit = 300;
  72. var
  73.     FileName: fnam ;
  74.     ioinname,ioutname: fnam;  {var type is defined in chcasw.pas}
  75.     auxflag, IsDirty, IsNewFile: Boolean;
  76.     itype,ierr,iochek,forgetit: integer;
  77.     inok,outok,oktogo: boolean;
  78.  
  79.     mystring: string;
  80.  
  81. type
  82.   TMyApplication = object(TApplication)
  83.     procedure InitMainWindow; virtual;
  84.   end;
  85.  
  86. type
  87.   PMyWindow = ^TMyWindow;
  88.   TMyWindow = object(TWindow)
  89.    constructor Init(AParent: PWindowsObject; ATitle: PChar);
  90.     destructor Done; virtual;
  91.  
  92.     procedure GO(var Msg: Tmessage); virtual cm_First+idm_Go;
  93.     function CanClose: Boolean; virtual;
  94.     procedure FileNew(var Msg: TMessage);
  95.       virtual cm_First + cm_New;
  96.     procedure FileOpen(var Msg: TMessage);
  97.       virtual cm_First + cm_Open;
  98.     procedure FileSave(var Msg: TMessage);
  99.       virtual cm_First + cm_Save;
  100.     procedure FileSaveAs(var Msg: TMessage);
  101.       virtual cm_First + cm_SaveAs;
  102.     function Nexistq:boolean;
  103.     function Fexistq:boolean;
  104.     procedure Help(var Msg: TMessage);
  105.       virtual cm_First + cm_Help;
  106.     procedure alldone(var Msg: Tmessage); virtual cm_First+cm_myexit;
  107.   end;
  108.  
  109. {--------------------------------------------------}
  110. { TMyWindow's method implementations:               }
  111. {--------------------------------------------------}
  112.  
  113. constructor TMyWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  114. begin
  115.   TWindow.Init(AParent, ATitle);
  116.   Attr.Menu := LoadMenu(HInstance,'menu_1');
  117.  
  118.   inok := false;
  119.   outok := false;
  120.   oktogo := false;
  121.   ierr := 0;
  122.   while ierr < 64 do
  123.   begin
  124.      ioinname[ierr] := ' ';
  125.      ioutname[ierr] := ' ';
  126.   inc(ierr)
  127.   end;
  128. end;
  129.  
  130. { -------------------------------------------------------------- }
  131.  
  132. destructor TMyWindow.Done;
  133. begin
  134.   TWindow.Done;
  135. end;
  136.  
  137. { -------------------------------------------------------------- }
  138.  
  139. procedure TMyWindow.alldone(var Msg: TMessage);
  140. begin
  141. if (canclose) then TMyWINDOW.DONE
  142. end;
  143.  
  144. { -------------------------------------------------------------- }
  145.  
  146. function tMYwindow.FEXISTQ: BOOLEAN;
  147.  
  148. {           *** checks to see if file exists ***       }
  149. {the file name is passed in global variable "filename" }
  150.  
  151. var
  152.  filstr: array[0..fsPathName] of Char;
  153.  filnam: string;
  154.  tempstr: array [0..48] of Char;
  155.  
  156. label endit;
  157.  
  158. begin
  159.  
  160. filnam := strpas(filename);
  161.  
  162.   filesearch(filstr,filename,GetENvVar('PATH'));
  163.   if (filstr[0] <> #0) then
  164.      auxflag := true
  165.   else
  166.      auxflag := false;
  167.  
  168. if (auxflag) then    {the file DOES EXIST! }
  169. begin
  170. fexistq := true;
  171.  
  172.   TEMPSTR[0] := #0;      {there is probably a much slicker way to}
  173.   strcat(tempstr,'');    {get the tempstr array put togetger}
  174.   strcat(tempstr,'Destroy file: ');
  175.   strcat(tempstr,filename);
  176.   strcat(tempstr,' ?');
  177.  
  178.   {not real slick...just aborts on NO.Doesn't ask for new fname}
  179.  
  180.  forgetit :=  MessageBox(Hwindow,tempstr,
  181.        '* File Already Exists! *',MB_YESNOCANCEL+mb_ICONQUESTION);
  182.  
  183.  if (forgetit = id_yes) then
  184.     begin
  185.        auxflag := false;  {or lie and say that it doesn't}
  186.        goto endit;
  187.     end;
  188.  
  189.   if (forgetit = id_cancel) or (forgetit = id_no) then
  190.   begin
  191.      auxflag := true;
  192.      goto endit;
  193.  
  194.   end;
  195.  
  196.  end; {of if forgetit = id_ok ?}
  197.  
  198.  
  199. endit:
  200.   fexistq := auxflag;
  201. end;
  202. { -------------------------------------------------------------- }
  203. { -------------------------------------------------------------- }
  204.  
  205. function tMYwindow.NEXISTQ: BOOLEAN;
  206.  
  207. {           *** checks to see if file exists ***       }
  208. {the file name is passed in global variable "filename" }
  209.  
  210. var
  211.  filstr: array[0..fsPathName] of Char;
  212.  filnam: string;
  213.  tempstr: array [0..48] of Char;
  214.  
  215. label endit;
  216.  
  217. begin
  218.  
  219. filnam := strpas(filename);
  220.  
  221.   filesearch(filstr,filename,GetENvVar('PATH'));
  222.   if (filstr[0] <> #0) then
  223.      nexistq := true
  224.   else
  225.   begin
  226.      nexistq := false;
  227.   end;
  228. end;
  229.  
  230.  
  231. { -------------------------------------------------------------- }
  232.  
  233. function TMyWindow.CanClose: Boolean;
  234. var
  235.   Reply: Integer;
  236. begin
  237.   CanClose := True;
  238.  
  239. (*
  240.   Reply := MessageBox(HWindow, 'Do you want to save?',
  241.     'Drawing has changed', mb_YesNo or mb_IconQuestion);
  242.   if Reply = id_Yes then CanClose := False;
  243. *)
  244.  
  245. end;
  246.  
  247. { -------------------------------------------------------------- }
  248.  
  249. procedure TMyWindow.FileNew(var Msg: TMessage);
  250. begin
  251. (* Just a dummy .. copied from BORLAND demo *)
  252. end;
  253.  
  254. { -------------------------------------------------------------- }
  255.  
  256. procedure TMyWindow.FileOpen(var Msg: TMessage);
  257. var
  258. areply: integer;
  259.  
  260. begin
  261.   areply := Application^.ExecDialog(New(PFileDialog,
  262.     Init(@Self, PChar(sd_FileOpen), StrCopy(ioinname, '*.*'))));
  263.  
  264.     filename := ioinname;
  265.     if (nexistq) then
  266.       inok := true
  267.     else
  268.       begin
  269.        messagebox(Hwindow,
  270.            'Can not find that file. Please choose another one.',
  271.             ioinname,mb_ok);
  272.        inok := false;
  273.     end;
  274. end;
  275.  
  276. { -------------------------------------------------------------- }
  277.  
  278. procedure TMyWindow.FileSave(var Msg: TMessage);
  279. begin
  280.   MessageBox(HWindow, 'Feature not implemented', 'FileSave', mb_Ok);
  281. end;
  282.  
  283. { -------------------------------------------------------------- }
  284.  
  285. procedure savefile;
  286.  
  287. begin
  288. (*
  289.    assign(iouttx,filename);
  290.    rewrite(iouttx); {unconditional file open.erases existing file}
  291. *)
  292. (* In this application, the FORTRAN DLL will actually do the writing
  293.    so all we want to do here is to OPEN THE FILE with KNOWN ID IOUT
  294. *)
  295.  
  296. (*
  297.    Points^.ForEach(@writit); {save everything in the POINTS stucture}
  298.  
  299.    close(iout);              {close the output file}
  300.  
  301.    isdirty := false;
  302. *)
  303. end;
  304. { ------------------------------------------------------------- }
  305.  
  306. procedure tmYwindow.FileSaveAs(var Msg: TMessage);
  307.  
  308. var
  309.   FileDlg: PFileDialog;
  310.   reply,areply: integer;
  311.   auxflag: boolean;
  312.  
  313.   label abegin;
  314.  
  315. begin
  316.   abegin:
  317.   StrCopy(IoutName, '');
  318.   reply :=  Application^.ExecDialog(New(PFileDialog,
  319.       Init(@Self, PChar(sd_FileSave), IoutName)));
  320.   filename :=ioutname;
  321.   if (reply = id_Ok) then
  322.      begin
  323.        auxflag := fexistq;
  324.            if not(auxflag) then
  325.            begin
  326.              outok := true;
  327.              SaveFile;
  328.            end;
  329.  
  330.            if (auxflag) then
  331.            begin
  332.            if (forgetit <> id_cancel) then
  333.               goto abegin; {ask for another name}
  334.            end;
  335.      end;
  336. end;
  337.  
  338. { -------------------------------------------------------------- }
  339.  
  340. procedure TMyWIndow.GO(Var MSg: Tmessage);
  341.  
  342. (* THIS ROUTINE IS THE ONE WHICH ACTUALLY CALLS THE FORTRAN ROUTINE *)
  343.  
  344. begin
  345. if ( (inok) and (outok) ) then
  346. begin
  347.    oktogo := true;
  348.    itype := 1;
  349.  
  350.    (* now call the FORTRAN subroutine CHCASE compiled into CHCASW.DLL *)
  351.  
  352.   chcase(IOINNAME,IOUTNAME,itype,ierr,iochek)    ;
  353.  
  354.    (* check error flags returned by CHCASE *)
  355.  
  356.     if ierr = 0 then
  357.       messagebox(Hwindow,'CHCASE run was successful. ','* CHCASE *',mb_ok);
  358.  
  359.     if ierr <> 0 then
  360.     begin
  361.        str(iochek:5,mystring); {reconvert to fixed str}
  362.        mystring :='CHCASE: IOCHECK = '+mystring;
  363.        MessageBox(HWindow,@mystring[1], ioinName,  mb_ok);
  364.     end;
  365.  
  366. end;
  367.  
  368. if not(oktogo) then
  369.   begin
  370.       if not(inok) then
  371.        messagebox(Hwindow,'No input file yet chosen!',' ??? ', mb_ok);
  372.  
  373.       if not(outok) then
  374.        messagebox(Hwindow,'No output file yet chosen!',' ??? ',mb_ok);
  375.    end;
  376.  
  377.     (* reset run check flags *)
  378.  if (oktogo) then
  379.     begin
  380.     oktogo := false;
  381.     inok := false;
  382.     outok := false;
  383.     end;
  384.  
  385. end;
  386. { -------------------------------------------------------------- }
  387.  
  388. procedure TMyWindow.Help(var Msg: TMessage);
  389. var
  390.   HelpWnd: PWindow;
  391. begin
  392.   (*
  393.   HelpWnd := New(PWindow, Init(@Self, 'Help System'));
  394.   with HelpWnd^.Attr do
  395.   begin
  396.     Style := Style or ws_Visible or ws_PopupWindow or ws_Caption;
  397.     X := 100;
  398.     Y := 100;
  399.     W := 300;
  400.     H := 300;
  401.   end;
  402.   Application^.MakeWindow(HelpWnd);
  403. *)
  404. end;
  405.  
  406. {--------------------------------------------------}
  407. { TMyApplication's method implementations:         }
  408. {--------------------------------------------------}
  409.  
  410. procedure TMyApplication.InitMainWindow;
  411. begin
  412.   MainWindow := New(PMyWindow, Init(nil, 'Sample ObjectWindows Program'));
  413. end;
  414.  
  415. {--------------------------------------------------}
  416. { Main program:                                    }
  417. {--------------------------------------------------}
  418.  
  419. var
  420.   MyApp : TMyApplication;
  421.  
  422. begin
  423.   MyApp.Init('MyProgram');
  424.   MyApp.Run;
  425.   MyApp.Done;
  426. end.
  427.